define(['config','text!app/module/express/print.html'],function(config,viewHTML){

    function contactItem(data) {
    	        //avalon.log(data);
                this.id = data.id;
                this.sender_company = data.sender_company;
                this.sender_address = data.sender_address;
                this.sender_phone = data.sender_phone;
                this.recipient_name = data.recipient_name;
                this.recipient_address = data.recipient_address;
                this.recipient_phone = data.recipient_phone;
                
    }

    var model = avalon.define({
    	       $id: "ContactPanel",
               id: "0",
               username: "",
               name:"面单",
               contactlists: [],
               initUsers: function() {
                      
                      $.ajax("http://127.0.0.1:8888/services/sheets/list", {

	                  type: "GET",
	       
	                  xhrFields: {

	                    withCredentials: true,
	                    useDefaultXhrHeader: false
	                  },

	                  crossDomain: true,

	                  success: function(data, status, xhr) {
	                     //avalon.log(data);
		                 var contactLists = $.map(data, function (item) {
		                    return new contactItem(item)
		                 });
		                 model.contactlists.pushArray(contactLists);
		                 //avalon.log(model.contactlists);
		                 //model.name = "快递面单";
	                  },
	                  error: function (responseData, textStatus, errorThrown) {
	                        alert("Failed: "+responseData);
	                    }
	              
	                });
               },
               addContact: function () {
 
                   var newResult = $("#addaPanel input").serialize(); 
                   var newContact = decodeURI(objToString(newResult));
                    
                   $.ajax({
                       type: "POST",
                       url: 'Contact.aspx/AddContact',
                       data: "{data:" + newContact + "}",
                       contentType: "application/json; charset=utf-8",
                       success: function (result) {
 
                       vm.contactlists.unshift(
                           new contactItem({
                               id:result.d,
                               contactname:vm.contactname,
                               tel: vm.tel,
                               address:vm.address
                           }));
                           vm.contactname = "";
                           vm.tel = "";
                           vm.address = "";
                       },
                       error: function (err) {
                           alert("添加失败")
                       }
                   });
                 },
 
               delContact: function (el) {
               
                   $.ajax({
                       type: "POST",
                       url: 'Contact.aspx/DeleteContact',
                       data: "{data:"+JSON.stringify(el.$model)+"}",
                       contentType: "application/json; charset=utf-8",
                       success: function (result) {
                           vm.contactlists.remove(el);
                       },
                       error: function (err) {
                           alert("删除失败");
                       }
                   });
               },
               openfiledialog: function () {
      			  $('#fileToUpload').click(); 
               },
               uploadfile: function () {
      			  upload(); 
               }
 
    });

    //model.initUsers();
    
    // 定义视图模块
	var viewmod =  {
	    viewtSrc: viewHTML,
		viewInit:function(){},   //模块view加载完成后的回调方法，在刷新（F5）时可能调用两次
		viewDestroy:function(){} //切换菜单时销毁原来的view
	 };

	 return viewmod;
});